// pages/signData/index.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currSelectDate: `${new Date().getFullYear()}-${new Date().getMonth()+1}-${new Date().getDate()}`,
        isShowDate: true,
        user_date_log: [],
        activeNames: [],
        timer: "",
        tineLong: 0,
        mineLong: 0,
        data: [],
        child_id: "",
        todayDate: '',
        showSign: false,
        startTime: ""
    },
    formatDate(str) {
        // 根据 - 符号拆分
        return str
            .split("-")
            .map((item) => {
                // +item 将item字符串转换为数字
                // 小于10的时候就补全一个前缀0
                if (+item < 10) {
                    return "0" + +item;
                }

                // 大于10的时候不用补0
                return item;
            })
            .join("-"); // 最后重组回来
    },
    //微日期组件选中日期
    onSelectDate(e) {
        // console.log('点击的日期：', e.detail.currDayDetail)
        console.log(e.detail);
        let date = this.formatDate(e.detail)
        this.setData({
            currSelectDate: date,
            timer: this.getNowTime(date),
        })
        // console.log(this.getNowTime(e.detail));
        this.getSignRecord(this.data.currSelectDate, this.data.child_id)

    },
    //获取当前时间
    getNowTime(time) {
        var date = new Date(time);
        //年 getFullYear()：四位数字返回年份
        var year = date.getFullYear(); //getFullYear()代替getYear()
        //月 getMonth()：0 ~ 11
        var month = date.getMonth() + 1;
        //日 getDate()：(1 ~ 31)
        var day = date.getDate();
        // //时 getHours()：(0 ~ 23)
        // var hour = date.getHours();
        // //分 getMinutes()： (0 ~ 59)
        // var minute = date.getMinutes();
        // //秒 getSeconds()：(0 ~ 59)
        // var second = date.getSeconds();
        var a = new Array("日", "一", "二", "三", "四", "五", "六");
        var week = new Date(time).getDay();
        var str = "星期" + a[week];
        var time = year + '.' + this.addZero(month) + '.' + this.addZero(day) + ' ' + str;
        var startTime = year + '-' + this.addZero(month) + '-01';
        var todayDate = year + '-' + this.addZero(month) + '-' + this.addZero(day)
        this.setData({
            todayDate: todayDate,
            startTime: startTime
        })
        return time;
    },
    //小于10的拼接上0字符串
    addZero(s) {
        return s < 10 ? ('0' + s) : s;
    },
    getSignRecord(date, child_id) {
        function toHourMinute(minutes) {
            return ((minutes / 60));
        }

        http.postHttp(api.getSignRecord + child_id, {
                date: date
            },
            (result) => {
                if (result.code == 0) {
                    result.data.records.forEach(ele => {
                        if (ele.id == 1) {
                            ele.long_time = toHourMinute(ele.long_time)
                            ele.icon = 'https://pcsys.admin.ybc365.com/72efb566-f6ab-4d3e-b0dc-e0fc3adc6947.png'
                        }
                        if (ele.id == 2) {
                            ele.icon = 'https://pcsys.admin.ybc365.com/667f6e60-eab0-416a-9c36-6a0c340dda6f.png'
                        }
                        if (ele.id == 3) {
                            ele.icon = 'https://pcsys.admin.ybc365.com/df9dc597-ddd7-464a-bbfd-8427c3b69d3c.png'
                        }
                    })
                    this.setData({
                        data: result.data.records
                    })

                }
            },
            (err) => {
                console.log(err);
            },
        )

    },
    // 去除0 参数 日期 如 2020-07-08 返回为 2020-7-8
    dislodgeZero(str) {
        let strArray = str.split("-");
        strArray = strArray.map(function (val) {
            if (val[0] == "0") {
                return (val = val.slice(1));
            } else {
                return val;
            }
        });
        return strArray.join("-");
    },
    /** 查询当月打卡日期数组 */
    getRecordDayData(date) {
        http.postHttp(api.getRecordDayData + this.data.child_id, {
                date: date
            },
            (result) => {
                if (result.code == 0) {
                    let arr = result.data.map(ele => {
                        return this.dislodgeZero(ele)
                    })
                    this.setData({
                        user_date_log: arr,
                        showSign: true
                    })
                    console.log(this.data.user_date_log);
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const childInfo = wx.getStorageSync('childInfo')
        this.setData({
            timer: this.getNowTime(new Date()),
            child_id: childInfo.id
        })
        this.getSignRecord(this.data.todayDate, this.data.child_id)
        this.getRecordDayData(this.data.startTime)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})